package com.cy.pj.arithmetic.service;

import com.cy.pj.common.exception.ServiceException;
import org.springframework.stereotype.Service;

/**
 * @author 张琪
 */
@Service
public class ArithmeticService {


/*    public int sum(int a, int b) {
        if(a < 0 || b < 0){
            throw new ServiceException("参数值不能为负数!!!");
        }
        return a + b;
    }*/

//    可变参数形式的代码
    public int sum(Integer... nums){
        if(nums == null || nums.length == 0){
//            可能出现的异常直接丢出
            throw new ServiceException("数组不能为空");
        }
        int sum = 0;
        for(int i = 0; i < nums.length; i++){
            sum += nums[i];
        }
        return sum;
    }

}
